import { loading, getSystemInfoSync } from '@doctorwork/utils';

import router from './business/router';
import request from './business/request';
import storage from './business/storage';
import payment from './business/payment';
import error from './business/error';
import msg from './business/msg';
import share from './business/share';
import block from './business/block';

import analyse, { AnalyseMiddleware } from './business/analyse';

// subpackage

import auth from './pages/login';
import auth_h5 from './pages/_login';
import web from './pages/web';

import mine from '../pages/mine/model'; //我的
import index from '../pages/index/model'; //今日油价
import price from '../pages/price/model'; //今日油价
import create from '../pages/create/model'; //今日油价
import detail from '../pages/detail/model'; //加油数据详情
import car from '../pages/car/model'; //加油数据详情
import oilList from '../pages/oilList/model'; //加油数据详情
import tipList from '../pages/tipList/model'; //加油数据详情
import tipDetail from '../pages/tipDetail/model'; //加油数据详情
import station from '../pages/station/model'; //加油数据详情
import report from '../pages/report/model'; //加油数据详情
import createMaintain from '../pages/createMaintain/model'; //加油数据详情
import maintenance from '../pages/maintenance/model';
import record from '../pages/record/model';
import recordList from '../pages/recordList/model';

const appModel = {
    namespace: '$app',
    state: {}
};

//公共服务
const builtin = [
    appModel,
    router,
    storage,
    error,
    web,
    request,
    auth,
    auth_h5,
    analyse,
    block,
    payment,
    msg,
    share
];

//业务模块
export default builtin.concat([ 
    index,
    mine,
    price,
    create,
    detail,
    car,
    oilList,
    tipList,
    tipDetail,
    station,
    report,
    createMaintain,
    maintenance,
    record,
    recordList,
]);

/**
 * @FIXME: 去掉，改用 $app 中的状态
 */
const sysInfo = getSystemInfoSync();
const matchSys = new RegExp('iOS', 'ig');
const isIos = matchSys.test(sysInfo.system);

let loadingFlag = false;
export const hooks = {
    onStateChange: state => {
        if (state.loading.global) {
            //ios直接showloading
            (!loadingFlag || isIos) && loading.show();
            loadingFlag = true;
        } else {
            loading.hide();
            loadingFlag = false;
        }
    },
    // @DANGER: 必须保留onError ，避免 promise.reject 结束后续调用
    onError() {}
};

const onAction = [
    // 打点插件
    AnalyseMiddleware
];

const $app = {
    systeminfo: sysInfo,
    preset: {
        weapp: {
            dev: 'develop', //开发版
            qa: 'trial', //体验版
            pre: 'trial',
            prod: 'release' //正式
        }
    },
    env: process.env.APP_ENV,
    device: {
        // 标志位： iPhone X
        iPhoneX: sysInfo.model == 'iPhone X'
    }
};

$app.preset.weappEnv = $app.preset.weapp[$app.env];
$app.isIos = matchSys.test($app.systeminfo.system);

/**
 * 添加额外信息
 * @param {*} opt
 */
export const merge = opt => {
    return {
        ...opt,
        ...hooks,
        // 初始状态
        initialState: {
            $app
        },
        onAction: (opt.onAction || []).concat(onAction)
    };
};

export const except = ['$request/get', '$storage/get', 'analyse/set'];
